package com.example.viewtest.FragmentGroup;

import android.graphics.drawable.Drawable;
import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.viewtest.AdapterGroup.Adapter_FragmentOne;
import com.example.viewtest.AdapterGroup.Adapter_FragmentThree;
import com.example.viewtest.AdapterGroup.HeaderBottomAdapter;
import com.example.viewtest.Dao.Bean;
import com.example.viewtest.R;
import com.jcodecraeer.xrecyclerview.ProgressStyle;
import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link FragmentThree#newInstance} factory method to
 * create an instance of this fragment.
 */
public class FragmentThree extends Fragment {

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    private List<Bean> list = new ArrayList<>();
    private XRecyclerView mRecyclerView;
    private Adapter_FragmentThree adapterFragmentThree;
    GridLayoutManager gridLayoutManager;


    public FragmentThree() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment FragmentThree.
     */
    // TODO: Rename and change types and number of parameters
    public static FragmentThree newInstance(String param1, String param2) {
        FragmentThree fragment = new FragmentThree();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_three, container, false);
        mRecyclerView = view.findViewById(R.id.recyclerview_fragmentThree);
        //初始化数据
        initRecyclerViewData();

        //设置上下拉刷新
        mRecyclerView.setPullRefreshEnabled(true);
        mRecyclerView.setLoadingMoreEnabled(true);
        //设置上下拉刷新样式
        mRecyclerView.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader);
        mRecyclerView.setLoadingMoreProgressStyle(ProgressStyle.BallRotate);
        mRecyclerView.setArrowImageView(R.drawable.icon_wait_loading);
        //创建和设置布局管理器
        //Grid布局
        gridLayoutManager=new GridLayoutManager(requireContext(), 2);//2代表一行放2个view
        mRecyclerView.setLayoutManager(gridLayoutManager);//这里用线性宫格显示 类似于grid view
        mRecyclerView.setAdapter(adapterFragmentThree=new Adapter_FragmentThree(requireContext(),list));


            gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
                @Override
                public int getSpanSize(int position) {
                    return (adapterFragmentThree.isHeaderView(position)) ? gridLayoutManager.getSpanCount() : 1;
                }
            });
        mRecyclerView.getDefaultFootView().setNoMoreHint("自定义加载完毕提示");
        mRecyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                new Handler().postDelayed(new Runnable(){
                    @Override
                    public void run() {
                        Bean bean = new Bean();
                        bean.setName("下拉新增");
                        bean.setImageId(R.drawable.landscape5);
                        list.add(0,bean);
                        mRecyclerView.refreshComplete();
                        adapterFragmentThree.notifyDataSetChanged();
                    }
                }, 1000);

            }

            @Override
            public void onLoadMore() {
                new Handler().postDelayed(new Runnable(){
                    @Override
                    public void run() {
                        Bean bean = new Bean();
                        bean.setName("上拉新增");
                        bean.setImageId(R.drawable.landscape1);
                        list.add(bean);
                        mRecyclerView.loadMoreComplete();
                        adapterFragmentThree.notifyDataSetChanged();
                    }
                }, 1000);
            }
        });
        //设置适配器
        mRecyclerView.setAdapter(adapterFragmentThree = new Adapter_FragmentThree(requireContext(),list));

        DividerItemDecoration itemDecoration = new DividerItemDecoration(requireContext(),DividerItemDecoration.VERTICAL);
        Drawable drawable = getResources().getDrawable(R.drawable.draw_divider);
        itemDecoration.setDrawable(drawable);
        mRecyclerView.addItemDecoration(itemDecoration);

        return view;
    }
    public void initRecyclerViewData(){
        Random random = new Random();
        for (int i=0;i<30;i++){
            Bean bean = new Bean();
            bean.setName("Item" + i);
//            bean.setType((random.nextInt()*2)+3);//[3,5)
//            Log.d("随机数生成监控","-----: "+bean.getType());
            switch (i%4){
                case 1:
                    bean.setImageId(R.drawable.game1);
                    break;
                case 2:
                    bean.setImageId(R.drawable.game2);
                    break;
                case 3:
                    bean.setImageId(R.drawable.game3);
                    break;
                case 0:
                    bean.setImageId(R.drawable.landscape5);
                    break;
                default:
            }
            list.add(bean);
        }
    }
}